package factor_0820;

public class MethodRecursion {
    public static void main(String[] args) {
        System.out.println(add(5));
        System.out.println(factor(5));
        System.out.println(sum(7685));
        printNum(1532);
        System.out.println();
        printReNum(1532);
        System.out.println();
        long strat=System.nanoTime();
        System.out.println(fibo(48));
        long end=System.nanoTime();
        System.out.println((end-strat)/1000000.0+"ms");

    }
    public static int add(int n){
        if(n==1){
            return 1;
        }
        return n+add(n-1);
    }
    public static int factor(int n){
        if(n<=2)
            return n;
        return n*factor(n-1);
    }
    public static int sum(int n){
        if(n<=9){
            return n;
        }
        return n%10+(sum(n/10));
    }
    public static void printNum(int n){
        if(n<10){
            System.out.print(n+" ");
            return;
        }
        printNum(n/10);
        System.out.print(n%10+" ");
    }
    public static void printReNum(int n){
        if(n<10){
            System.out.print(n+" ");
            return;
        }
        System.out.print(n%10+" ");
        printReNum(n/10);

    }
    public static int fibo(int n){//斐波那契数
//        if(n<3)
//            return 1;
//        return fibo(n-1)+fibo(n-2);
        int last1=1;
        int last2=1;
        int cur=0;
        for(int i=3;i<=n;i++){
            cur=last1+last2;
            last1=last2;
            last2=cur;
        }
        return cur;
    }

}
